package com.example.nft.controller;


import com.baomidou.mybatisplus.core.conditions.Condition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nft.dao.TutuMapper;
import com.example.nft.entity.Tutu;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (Tutu)表控制层
 *
 * @author makejava
 * @since 2022-04-07 17:59:37
 */
@RestController
@RequestMapping("tutu")
public class TutuController {
    @Resource
    public TutuMapper tutuMapper;


    @ApiOperation (value = "查询")
    @GetMapping("/select")
    public List<Tutu> selectAll(String fenqu,String name){
        QueryWrapper<Tutu> queryWrapper = new QueryWrapper<> ();
        if (fenqu!=null&&!("".equals(fenqu))) {
            queryWrapper.like ("fenqu", fenqu);
        }
        if (name!=null&&!("".equals(name))) {
            queryWrapper.like ("name", name);
        }

        List<Tutu> tutus = tutuMapper.selectList (queryWrapper);
        return tutus;
    }

//    @ApiOperation(value = "插入")
//    @GetMapping("/testDemo")
    public String testDemo(){
        String video1 =null;
        List<String> videohtmls;
        String url = "https://www.b4n66.com/";
        List<String> htmls = getHtmls (url);
        for (String s : htmls) {
            if (s.contains (":void(0)")){
                continue;
            }
            videohtmls = getVideoHtml (s);
            for (String videohtml : videohtmls) {
                if (s.contains (":void(0)")){
                    continue;
                }
                video1= getVideo(videohtml);
            }
        }
        return video1;
    }
    public String getVideo(String html){
        String html1 = getHtml(html);
        Document document = Jsoup.parse(html1);
        Elements wrap = document.select (".download");
        String title = document.getElementsByTag("title").get(0).text();
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                Tutu tutu = new Tutu();
                tutu.setAddress(attr);
                tutu.setName(title);
                tutuMapper.insert(tutu);
            }
        }
        return "插入成功";
    }
    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
    public static Integer getindex(String html){
        String html1 = getHtml(html);
        String s ="https://www.b4n66.com";
        Document document = Jsoup.parse(html1);
        Elements a1 = document.select(".moble_pagination").select("a");
        String index =s+a1.get(a1.size() - 1).attr("href");
        String index_ = index.replace(html, "").replace("index_", "").replace(".html", "");
        return Integer.valueOf(index_);
    }
    public static List<String> getVideoHtml(String html){
        Integer getindex = getindex(html);
        List<String> list = new ArrayList<> ();
        for (int i = 1; i<=getindex; i++){
            StringBuilder htmlBuilder = new StringBuilder(html);
            htmlBuilder.append("index_").append(i).append(".html");
            String s = htmlBuilder.toString();
            s = getHtml(s);
            Document document = Jsoup.parse(s);
            Elements wrap = document.select (".preview-item");
            for (Element element : wrap) {
                Elements a = element.select ("a");
                for (Element element1 : a) {
                    String attr = element1.select ("a").attr ("href");
                    list.add ("https://www.b4n66.com"+attr);
                }
            }
        }
        return list;
    }
    public static List<String> getHtmls(String html){
        html = getHtml(html);
        List<String> list = new ArrayList<> ();
        String s =null;
        Document document = Jsoup.parse(html);
        Elements wrap = document.select (".wrap");
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                list.add ("https://www.b4n66.com"+attr);
            }
        }
        return list;
    }
}

